package com.hycat.mail.common.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author 吕泽浩业
 * @version 1.0
 */
public class DateUtil {

    private final static String dateTimePattern = "yyyy-MM-dd HH:mm:ss";
    private final static String datePattern = "yyyy-MM-dd";
    private final static DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(dateTimePattern);
    private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(datePattern);
    public final static String[][] days = {{"31","28","31","30","31","30","31","31","30","31","30","31"},
            {"31","29","31","30","31","30","31","31","30","31","30","31"}};

    public final static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getCurrentTime(){
        return LocalDateTime.now().format(timeFormatter);
    }

    public static String getCurrentDate(){
        return LocalDate.now().format(dateFormatter);
    }

    //获得当月第1天的00:00:00
    public static String getStartDay(String today) {
        return today.substring(0, 8) + "01 00:00:00";
    }

    //获得当月最后一天的23:59:59
    public static String getEndDay(String today) {
        int year = Integer.parseInt(today.substring(0, 4));
        int month = Integer.parseInt(today.substring(5, 7));
        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return today.substring(0, 8) + days[1][month - 1] + " 23:59:59";
        } else {
            return today.substring(0, 8) + days[0][month - 1] + " 23:59:59";
        }
    }
}
